package cn.tedu.mvc.config;

import cn.tedu.mvc.controller.TimeAccessInterceptor;
import cn.tedu.mvc.web.filter.DataFilter;
import cn.tedu.mvc.web.listener.AppListener;
import cn.tedu.mvc.web.servlet.DataServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringWebConfig implements WebMvcConfigurer {
    public SpringWebConfig(){
        System.out.println("SpringWebConfig()");
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TimeAccessInterceptor())
                 //定义要拦截的路径
                .addPathPatterns("/api/**");
    }
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
       return new ServletListenerRegistrationBean(new AppListener());
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        //注册filter
        FilterRegistrationBean filterRegistrationBean =
                new FilterRegistrationBean(new DataFilter());
        //过滤所有请求
        filterRegistrationBean.addUrlPatterns("/api/*");
        return filterRegistrationBean;
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean bean=
                new ServletRegistrationBean(new DataServlet());
        bean.addUrlMappings("/data");
        return bean;
    }

}
